package sgi.client;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JOptionPane;

import sgi.details_new.ImageDetails;
import sgi.message.MessageCharge;
import sgi.message.MessageGetTariff;
import sgi.message.MessageGetTariff.tariff;
import sgi.message.MessagePurchaseImage;

public class SGIPurchaseImageController extends SGIController {

	public enum Source {search,map};
	private static SGIPurchaseImageController purchase;
	private static SGIPurchaseImageUI purchaseUI;
	private int imageid;
	private ImageDetails details;
	private Source src;
	
	public static SGIPurchaseImageController create(ImageDetails imageDetails,Source src) throws IOException
	{
		if(SGIPurchaseImageController.purchase == null)
		{
			SGIPurchaseImageController.purchase = new SGIPurchaseImageController();
			SGIPurchaseImageController.purchaseUI.setController(purchase);
			SGIPurchaseImageController.purchase.setImageid(imageDetails.getImageid());
			SGIPurchaseImageController.purchase.setDetails(new ImageDetails(imageDetails));
			purchase.src = src;
		}
		
		purchaseUI.setVisible(true);
		return purchase;
	}
	
	private SGIPurchaseImageController()
	{
		purchaseUI = new SGIPurchaseImageUI();
	}
	

	public int getImageid() {
		return imageid;
	}

	public void setImageid(int imageid) {
		this.imageid = imageid;
	}
	
	public static void terminate()
	{
		SGIPurchaseImageController.purchase = null;
		SGIPurchaseImageController.purchaseUI.setController(null);
		SGIPurchaseImageController.purchaseUI.setVisible(false);
		SGIPurchaseImageController.purchaseUI = null;
	}
	
	public void purchase()
	{
		MessagePurchaseImage msg = new MessagePurchaseImage(imageid);
		msg.setDetails(getDetails());
		try {
			SGIClientConnectionController.createConnection().handleMessageFromClientUI(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	void handleMessage(Object msg) {
		
			SGIPurchaseImageController.terminate();

		if(((MessagePurchaseImage)msg).isInsufficiantBalance() == true)
		{
			JOptionPane.showMessageDialog( src == Source.search ? SGISearchArchiveController.getUI() : SGIInteractiveMapController.getUI(),"Insufficiant balance.");
		}
		else if(((MessagePurchaseImage)msg).isImageAlreadyPurchased() == true )
		{
			JOptionPane.showMessageDialog( src == Source.search ? SGISearchArchiveController.getUI() : SGIInteractiveMapController.getUI(),"Image already purchased.");
		}
		else
		{
			JOptionPane.showMessageDialog( src == Source.search ? SGISearchArchiveController.getUI() : SGIInteractiveMapController.getUI(),"Image purchased successfuly.");
			new File(System.getProperty("user.home")+"/My Documents/SGIimages."+SGIUserAccountMenuController.getUser().getUsername()).mkdirs();
			byte[] buffer = ((MessagePurchaseImage)msg).getBlobAsBytes();
			InputStream input = new ByteArrayInputStream(buffer);
			try {
			  OutputStream output = new FileOutputStream(System.getProperty("user.home")+"\\My Documents\\SGIimages."+SGIUserAccountMenuController.getUser().getUsername()+"\\Image%" + String.valueOf(((MessagePurchaseImage)msg).getImageid()) + "%" + ((MessagePurchaseImage)msg).getDetails().getDate() + "%" + ((MessagePurchaseImage)msg).getDetails().getLocation() + "%" +".jpeg");
			  try {
			    int bytesRead;
			    while ((bytesRead = input.read(buffer)) != -1) {
			      output.write(buffer, 0, bytesRead);
			    }
			  } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
			    try {
					output.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			  }
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
			  try {
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			if(((MessagePurchaseImage)msg).isHasCommerceSubscription() == true)
			{
				buffer = ((MessagePurchaseImage)msg).getBlobAsBytesCommerce();
				input = new ByteArrayInputStream(buffer);
				try {
				  OutputStream output = new FileOutputStream(System.getProperty("user.home")+"\\My Documents\\SGIimages."+SGIUserAccountMenuController.getUser().getUsername()+"\\Commerce"+ "%" + String.valueOf(((MessagePurchaseImage)msg).getImageid()) + "%" + ((MessagePurchaseImage)msg).getDetails().getDate() + "%" + ((MessagePurchaseImage)msg).getDetails().getLocation() + "%" +".jpeg");
				  try {
				    int bytesRead;
				    while ((bytesRead = input.read(buffer)) != -1) {
				      output.write(buffer, 0, bytesRead);
				    }
				  } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
				    try {
						output.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				  }
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
				  try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
			}
			if(((MessagePurchaseImage)msg).isHasInfrastractureSubscription() == true)
			{
				buffer = ((MessagePurchaseImage)msg).getBlobAsBytesInfrastracture();
				input = new ByteArrayInputStream(buffer);
				try {
				  OutputStream output = new FileOutputStream(System.getProperty("user.home")+"\\My Documents\\SGIimages."+SGIUserAccountMenuController.getUser().getUsername()+"\\Infrastracture"+ "%" + String.valueOf(((MessagePurchaseImage)msg).getImageid()) + "%" + ((MessagePurchaseImage)msg).getDetails().getDate() + "%" + ((MessagePurchaseImage)msg).getDetails().getLocation() + "%" +".jpeg");
				  try {
				    int bytesRead;
				    while ((bytesRead = input.read(buffer)) != -1) {
				      output.write(buffer, 0, bytesRead);
				    }
				  } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
				    try {
						output.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				  }
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
				  try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
			}
			if((((MessagePurchaseImage)msg).isHasCommerceSubscription() == true)&&(((MessagePurchaseImage)msg).isHasInfrastractureSubscription() == true))
			{
				buffer = ((MessagePurchaseImage)msg).getBlobAsBytesCommerceInfrastracture();
				input = new ByteArrayInputStream(buffer);
				try {
				  OutputStream output = new FileOutputStream(System.getProperty("user.home")+"\\My Documents\\SGIimages."+SGIUserAccountMenuController.getUser().getUsername()+"\\CommerceInfrastracture"+ "%" + String.valueOf(((MessagePurchaseImage)msg).getImageid()) + "%" + ((MessagePurchaseImage)msg).getDetails().getDate() + "%" + ((MessagePurchaseImage)msg).getDetails().getLocation() + "%" +".jpeg");
				  try {
				    int bytesRead;
				    while ((bytesRead = input.read(buffer)) != -1) {
				      output.write(buffer, 0, bytesRead);
				    }
				  } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
				    try {
						output.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				  }
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
				  try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}	
			}

		}
	}

	public ImageDetails getDetails() {
		return details;
	}

	public void setDetails(ImageDetails details) {
		this.details = details;
	}
	
	public SGIPurchaseImageUI getUI()
	{
		return purchaseUI;
	}
	
	public Source getSource()
	{
		return src;
	}

}

